
function onSelectDgRole(index,row) {
    //清空原本树形数据

  /*  $('#menu_tree_role').tree('loadDate',{});*/
    $('#menu_tree_role').tree('loadData',{});
    
    init_menu_tree_role(row.id);
}
var  init_menu_tree_role = function (roleId) {

    $('#menu_tree_role').tree({
                                  url:ctx + "/sys/limit_loadMenuByRole?roleId="+roleId,
                                  checkbox:true,
                                  onBeforeCheck: function (node, checked){
                                      var selected = $('#dg_role').datagrid('getSelected');
                                      var idArr = [];
                                      findChildrenIds(node, idArr);
                                      //提交到后台保存，并返回是否保存成功
                                      return saveRoleLimit(checked,selected.id,idArr.join(','));
                                  }
                              })
}
var findChildrenIds = function (node, idArr) {
    var ids = node.id;
    idArr.push(ids);
    var children = node.children;
    if (!node.children) {
        return;
    }
    for (var i = 0; i < children.length; i++) {
        findChildrenIds(children[i], idArr);
    }
}

function  saveRoleLimit(checked,roleId,menuIds) {
    var isSuccess= false;
    $.ajax({
        url: ctx+'/sys/limit.roleMenu',
        type:'POST',
        data:{checked :checked ,roleId :roleId ,menuIds :menuIds},
        async:false,
        success:function (data) {
            if (data && data.code && data.code == 200) {
                isSuccess = true;
            }
            if (data && data.code && data.code != 200 && data.msg) {
                $.messager.alert('系统提示', data.msg);
            }
        },
               error: function (xhr) {
                   $.messager.alert('系统提示', "服务器连接失败");
               }

           });
return true;
}

